package com.bluefox.hospital.controller;

import com.bluefox.common.web.dto.result.PageResultDTO;
import com.bluefox.common.web.dto.result.ResultDTO;
import com.bluefox.hospital.convertor.RichTextConvertor;
import com.bluefox.hospital.dto.RichTextDTO;
import com.bluefox.hospital.model.RichText;
import com.bluefox.hospital.repository.RichTextRepository;
import com.bluefox.hospital.service.RichTextService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * RichText的管理接口
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/w/richText")
public class RichTextController {
    @Autowired
    private RichTextService richTextService;
    @Autowired
    private RichTextRepository richTextRepository;
    @Autowired
    private RichTextConvertor richTextConvertor;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @param type     查询类型
     * @return 分页数据
     */
    @RequestMapping(value = "/s", method = RequestMethod.POST)
    public PageResultDTO<RichTextDTO> search(@RequestParam(value = "type", required = false) String type, final Pageable pageable) {
        Pageable request = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().and(Sort.by(Sort.Order.desc("createdDate"))));
        Page<RichText> models=null;
        if(!StringUtils.isEmpty(type)){
           models = this.richTextService.findAll(type, request);
        }else{
            models = this.richTextService.findAll( request);
        }
        return this.richTextConvertor.toResultDTO(models);
    }

    /**
     *  前台查询方式
     * @param type
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public PageResultDTO<RichTextDTO> searchWeb(@RequestParam(value = "type", required = false) String type, final Pageable pageable) {
        Page<RichText> models=null;
        Pageable request = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().and(Sort.by(Sort.Order.desc("createdDate"))));

        if(!StringUtils.isEmpty(type)){
            models = this.richTextService.findAllWeb(type, request);
        }else{
            models = this.richTextService.findAllWeb( request);
        }
        return this.richTextConvertor.toResultDTO(models);
    }

    /**
     * 取得详细数据
     *
     * @param id 资源ID
     * @return 资源详细
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<RichTextDTO> get(@PathVariable final Long id) {
        final RichText model = this.richTextService.get(id);
        return this.richTextConvertor.toResultDTO(model);
    }

    /**
     * 新建操作
     *
     * @param richTextDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(method = RequestMethod.POST)
    public ResultDTO<RichTextDTO> create(@RequestBody @Valid final RichTextDTO richTextDTO) {

        final RichText model = this.richTextConvertor.toModel(richTextDTO);

        this.richTextService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", RichText.class.getSimpleName(), model.getId());
        }
        return this.richTextConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param id          更新资源的ID
     * @param richTextDTO 更新资源的DTO
     * @return 更新后资源
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<RichTextDTO> update(@PathVariable final Long id, @RequestBody @Valid final RichTextDTO richTextDTO) {
        richTextDTO.setId(id);
        final RichText model = this.richTextConvertor.toModel(richTextDTO);
        this.richTextService.update(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", RichText.class.getSimpleName(), model.getId());
        }
        return this.richTextConvertor.toResultDTO(model);
    }
    /**
     * 置顶操作
     *
     * @param richTextDTO 更新资源的DTO
     * @return 更新后资源
     */
    @RequestMapping(value = "/roofPlacement/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<RichTextDTO> roofPlacement( @PathVariable final Long id) {
        RichText model = this.richTextService.get(id);
        this.richTextService.roofPlacement(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", RichText.class.getSimpleName(), model.getId());
        }
        return this.richTextConvertor.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param Id 资源ID
     * @return 操作结果
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.richTextService.delete(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was deleted.", RichText.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }
    @RequestMapping(value = "/getTopThree", method = RequestMethod.GET)
    public List<RichTextDTO> getTopThree() {
        List<RichText> top6ByType = this.richTextService.findTop6ByType("1",1);
        return this.richTextConvertor.toListDTO(top6ByType);
    }

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @param type     查询类型 过滤未通过
     * @return 分页数据
     */
    @RequestMapping(value = "/findByTypeStatus", method = RequestMethod.POST)
    public PageResultDTO<RichTextDTO> findByTypeStatus(@RequestParam(value = "type", required = false) String type, final Pageable pageable) {
        Pageable request = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort().and(Sort.by(Sort.Order.desc("createdDate"))));

        final Page<RichText> models = this.richTextService.findByTypeAndStatus(type,1,request);
        return this.richTextConvertor.toResultDTO(models);
    }
}
